﻿@{
    Layout = "~/Views/Shared/_LayoutHaveManue.cshtml";
}
@using WxPayAPI;
@using AntDesigner.NetCore.GameCity;
@model IRoom
<style>
    #roomTools {
        display: flex;
    }
        #roomTools input {
            flex: 1;
        }
        #roomTools button {
            background-color: white;
            color: green;
            font-weight: bolder;
        }
        #roomTools li > button {
            background: linear-gradient(to bottom,#FFFF33 10%,#FFFF33, #CCFF33, #99FF33);
            width: 100%;
        }
        #roomTools ul {
           padding:0px;
        }
        #LeaveMenu, #SendMessage {
            font-weight: bolder;
        }

</style>
@section styles{
    @RenderSection("styles", required: false)
}
<!--房间信息 -->
<div class="modal fade" id="RoomInfoModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">房间信息</h4>
            </div>
            <div class="modal-body">
                <label>房间名称</label>@Model.Name<br />
                <label>门票价格</label>@Model.TicketPrice<br />
                <label>人数上限</label>@Model.Players.Count/@Model.PlayerCountTopLimit<br />
                <label>到期时间</label>@Model.Timelimit<br />
                <label>房间公告</label><br /><hr />@Model.Affiche<br />
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<!--提示信息 -->
<div class="modal fade" id="AlertInfoModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">提示信息</h4>
            </div>
            <div class="modal-body">
                <label id="alertContent"></label><br />
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<div class="modal fade" id="gameOverInfo" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">提示信息</h4>
            </div>
            <div class="modal-body">
                <label>游戏结束</label><br />
                <button id="gameOvergoon" class="btn btn-warning btn-xs  text-center buttnMargin">继续游戏</button><button id="gameOverleave" class="btn btn-warning btn-xs  text-center buttnMargin">离开房间</button>
            </div>
            <div class="modal-footer">
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<div class="modal fade" id="gameResetInfo" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">提示信息</h4>
            </div>
            <div class="modal-body">
                <label>本局游戏结束</label><br />
                <button id="gameReset" class="btn btn-warning btn-xs  text-center buttnMargin">继续游戏</button><button id="gameOverleave" class="btn btn-warning btn-xs  text-center buttnMargin">离开房间</button>
            </div>
            <div class="modal-footer">
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<div class="modal fade" id="gameStoped" tabindex="-1" role="dialog" data-backdrop="false" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">游戏终止信息</h4>
            </div>
            <div class="modal-body">
                <label id="stopMessage"></label><br />
                <button id="gameStopgoon" class="btn btn-warning btn-xs  text-center buttnMargin">继续游戏</button><button id="gameStopleave" class="btn btn-warning btn-xs  text-center buttnMargin">离开房间</button>
            </div>
            <div class="modal-footer">
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<div id="GamePanel" style="display:flex;flex-direction:column;height:95%;flex-wrap:wrap;width:100%">
    @RenderBody()
</div>
<div id="roomTools">
    <div class="btn-group dropup">
        <button type="button" id="LeaveMenu" class=" dropdown-toggle" data-toggle="dropdown">离开游戏</button>
        <ul class="dropdown-menu" role="menu">
            <li><button type="button" id="LeaveForRechage">游戏充值</button></li>
            @*<li><button type="button" id="ConfigRoom">配置房间</button></li>*@
            <li><button type="button" id="ShowRoomInfo">房间信息</button></li>
            <li><button type="button" id="leaveRoom">安全离开</button></li>
        </ul>
    </div>
    <input type="text" id="message" list="forMessage" placeholder="消息内容:" />
    <datalist id="forMessage">
        <option>快点!我等得花儿都谢了</option>
        <option>大家慢慢玩!我先离开了</option>
    </datalist>
    <button type="button" id="SendMessage">发送</button>
</div>
@section scripts{
    <script>
        var messageInput;
        var _gameFace = new GameFace();
        ///服务端方法签名
         function MethodInfo(methodName){
             this.askMethodName = methodName;
             this.methodParam = {};
        }
        ///调用服务端方法
         function InvokeSeverMethod(methodInfo) {
            var result;
            console.debug("invokeSeverMehtod_" + methodInfo.askMethodName);
            $.ajax({
                type: "Post",
                async:false,
                url: "/GameProjects/GameHandler/WebSocketHandler?gameCityId=@Model.GameCityId&roomId=@Model.Id",
                data: methodInfo,
                dataType: "json",
                success: function (data) {
                    result = data;
                }
            })
            return result;
         }
         function InvokeSeverMethodAsync(methodInfo) {
            var result;
            console.debug("invokeSeverMehtod_" + methodInfo.askMethodName);
            $.ajax({
                type: "Post",
                async:true,
                url: "/GameProjects/GameHandler/WebSocketHandler?gameCityId=@Model.GameCityId&roomId=@Model.Id",
                data: methodInfo,
                dataType: "json",
                success: function (data) {
                    result = data;
                }
            })
            return result;
         }
         var webSocket = new WebSocket("ws://@(WxPayConfig.SiteNameNopre)/ws");
        webSocket.onopen = function () {
            $.get("/GameProjects/GameHandler/WebsocketCheck");
            _gameFace.Fresh();
        }
        webSocket.onmessage = function (event) {
            var obj = JSON.parse(event.data);
            var methodName = obj.ClientMethodName;
            var paramStar="";
            $.each(obj, function(name, value){
                if (name != "ClientMethodName") {
                    paramStar=paramStar+"'"+value+"'"+","
                }
            });
            paramStar = paramStar.substring(0, paramStar.length - 1);
            paramStar = "(" + paramStar + ")";
            eval(methodName + paramStar);
        }
        webSocket.onerror = function () {
            alert("和游戏服务器的连接发生错误");
        }
        webSocket.onclose = function () {
            alert("发生错误,已和服务器断开连接");
        }
        //配置房间
        function ConfigRoom() {
            if (window.C_ConfigRoom) {
                C_ConfigRoom();
            } else {
                alert("配置房间功能不可用!");
            }
        }
        ///游戏界面
         function GameFace() {
            ///刷新
            this.Fresh = function (){
                console.debug("befor_GameFace");
                var methodInfo = new MethodInfo("FreshGameFace");
                var gameFace = InvokeSeverMethod(methodInfo);
                Fresh(gameFace);
            }
         }
        //开始游戏
        function StartGame(){
            console.debug("befor_StartGame");
            $.get("/GameProjects/GameHandler/Start?gameCityId=@Model.GameCityId&roomId=@Model.Id");
            console.debug("after_startGame");
        }
        //离开房间返回大厅
        function LeaveRoom() {
            //beforLeaveNoCheck = true;
            console.debug("befor_LeaveRoom");
            window.location.href ="/Citys/Rooms/LeaveRoom?gameCityId=@Model.GameCityId&roomId=@Model.Id";
            console.debug("after_LeaveRoom");
        }
          function LeaveRoomAjax(){
              console.debug("befor_LeaveRoom");
              var url = "/Citys/Rooms/LeaveRoom?gameCityId=@Model.GameCityId&roomId=@Model.Id";
              $.get(url);
            console.debug("after_LeaveRoom");
        }
        function   LeaveForRechage(){
              window.location.href ="/Player/Index_recharge/";
          }
        function LeaveForwithdrawCash()
        {
            window.location.href = "/Player/Index_withdrawCash /";
        }
        //开始新的一局
        function NewInnigeGame(){

        }
        ///提示信息
        function Alert(AlertContent){
            $("#AlertInfoModal").find("#alertContent").html(AlertContent);
            $("#AlertInfoModal").modal("show");
        }
        //游戏正常结束
        function GameOver(){
            $("#gameOverInfo").modal("show");
        }
        //游戏异常中断
        function Stoped(Message) {
            $("#stopMessage").text(Message);
            $("#gameStoped").modal("show");
        }
        ///重置游戏
        function ResetGame(){
            $("#gameResetInfo").modal("show");
        }
        //提玩家出房间
        function Kicket(event){
            var playerId = event.data.playerId;
            $.get("/Citys/Rooms/KickPlayer?playerId=" + playerId +"&gameCityId=@Model.GameCityId&roomId=@Model.Id");
        }
        function ShowRoomInfo() {//房间信息
                $('#RoomInfoModal').modal('show');
        }
        function GameStopgoon() {
            $("#gameStoped").modal("hide");
            if (window.C_GameStopgoon){
                C_GameStopgoon();
            }
        }
            function GameOvergoon() {
                $('#gameOverInfo').modal("hide");
                if (window.C_GameOvergoon){
                   C_GameOvergoon();
                }
        }
            function GameReset() {
                $('#gameResetInfo').modal("hide");
                if (window.C_GameReset){
                    C_GameReset();
                }
            }
        //房间消息
            function RoomMessage(name,message){
                if (window.C_RoomMessage) {
                    var messageChecked;
                    messageChecked = "<PRE>" + messageChecked + "</PRE>";
                    C_RoomMessage(name,message);
                }
            }
        //发送房间消息
            function SendMessage() {
                var messageStr = messageInput.text();
                messageStr = messageStr.replace("", "");
                if ( typeof(message)!="String"){
                    messageStr = $("#message").val();
                    if (messageStr.length==0) {
                        return;
                    }
                } else if(message!=""){
                    messages = message;
                } else {
                    return;
                }
                $.get("/Citys/Rooms/RoomMessage?gameCityId=@Model.GameCityId&roomId=@Model.Id&message="+messageStr);
            }
            function RoomSetlayOut(){
                var canv = $("canvas");
                $.each(canv, function () {
                    $(this).attr("width",$(this).parent("div").width());
                    $(this).attr("height",$(this).parent("div").height())
                });
            }
            function CleanCanv(canv, colr) {
                if (typeof (colr) == "undefined"){
                    colr = "palegreen";
                }
                var context = canv.getContext('2d');
                context.fillStyle = colr; //设置填充色
                context.fillRect(0, 0, canv.width, canv.height);
            }
                wx.config({
            debug: false,
            appId: '@Html.Raw(ViewBag.wxConfig.appId)',
            timestamp: '@Html.Raw(ViewBag.wxConfig.timestamp)',
            nonceStr: '@Html.Raw(ViewBag.wxConfig.nonceStr)',
            signature: '@Html.Raw(ViewBag.wxConfig.signature)',
            jsApiList: ["onMenuShareAppMessage", "onMenuShareTimeline"]
        });
        wx.ready(function () {
            wx.onMenuShareAppMessage({
                title: '来炸金花',
                desc: '点击进入',
                link: '@(Html.Raw(WxPayConfig.SiteName))/Game/Index/?state=@(Html.Raw(ViewBag.shareId))',
                imgUrl: '@(Html.Raw(WxPayConfig.SiteName))/images/zhajinghua.png',
                type: '',
                dataUrl: '',
                success: function () {
                },
                cancel: function () {
                    alert("求求你分享给朋友吧！主人");
                }
            });
            wx.onMenuShareTimeline({
                title: '来炸金花',
                link: '@(Html.Raw(WxPayConfig.SiteName))/Game/Index/?state=@(Html.Raw(ViewBag.shareId))',
                imgUrl:'@(Html.Raw(WxPayConfig.SiteName))/images/zhajinghua.png',
                success: function () {
                },
                cancel: function () {
                    alert("求求你分享给朋友吧！主人");
                }
            });
                });
        $(function () {
            RoomSetlayOut();
            $(window).resize(function () {
                RoomSetlayOut();
            });
         messageInput = $("#message");
         $("#leaveRoom").on("click", LeaveRoom);
         $("#ConfigRoom").on("click", ConfigRoom);
         $("#ShowRoomInfo").on("click", ShowRoomInfo);
         $("#SendMessage").on("click", SendMessage);
         $("#gameOvergoon").on("click",GameOvergoon );
         $("#gameStopgoon").on("click", GameStopgoon );
         $("#gameOverleave").on("click", LeaveRoom);
         $("#gameStopleave").on("click", LeaveRoom);
         $("#LeaveForRechage").on("click", LeaveForRechage);
        })
    </script>
    @RenderSection("scripts", required: false)
}


